package work.vcloud.baseinfo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import work.vcloud.baseinfo.utils.HttpUtil;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.util.JsonUtil;
import work.vcloud.frame.vo.OutputVo;

import javax.annotation.Resource;

@RestController
@Scope("prototype")
public class UserInfoController extends BaseController {

	@Value("${api.passwordReceive}")
	private String passwordReceive;

	@Value("${auth.app_id}")
	private String appId;
	
	@Resource
	OpenDao openDao;

	@RequestMapping("/user/getUserDetail")
	public Object getUserInfo() {
		try {
			return output(session.getAttribute("userInfo"));
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}


	@RequestMapping("/user/getUserInfoDetail")
	public Object getUserInfoDetail() {
		try {
			Map<String, Object> map = getParameterMap();
			map.put("sqlMapId", "userindex.select");

			return output(openDao.queryForObject(map));
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	@RequestMapping("api/user.resetPwd")
	public Object resetPwd() throws Exception {
		Map<String, Object> map = getParameterMap();
		String ids = map.get("ids").toString();
		String name = "";

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("ids",ids);
		jsonObject.put("type", 2);
		String accessToken = ((Map)session.getAttribute(appId)).get("access_token").toString();
		Object obj = HttpUtil.doPost2(passwordReceive, jsonObject, accessToken);

		if(obj != null) {
			JSONObject jsonData = jsonObject.parseObject(obj.toString());
			if(jsonData.getString("code").equals("0000")) {
				String data = jsonData.get("data").toString();
				if (!data.isEmpty()) {
					String[] dataStrs = data.split(",");
					List list = Arrays.asList(dataStrs);
					Map<String, Object> queryMap = getParameterMap();
					queryMap.remove("page");
					for (int i = 0; i < list.size(); i++) {
						if ("1".equals(map.get("type").toString())) {
							queryMap.put("id", list.get(i).toString());
							queryMap.put("sqlMapId", "teacher.select");
							List<Map<String, Object>> userList = openDao.queryForList(queryMap);
							if (userList.size() > 0) {
								name = name + "," + userList.get(0).get("name").toString() + "";
							}
						} else if ("2".equals(map.get("type").toString())) {
							queryMap.put("id", list.get(i).toString());
							queryMap.put("sqlMapId", "student.select");
							List<Map<String, Object>> userList = openDao.queryForList(queryMap);
							if (userList.size() > 0) {
								name = name + "," + userList.get(0).get("name").toString() + "";
							}
						} else if ("5".equals(map.get("type").toString())) {
							queryMap.put("id", list.get(i).toString());
							queryMap.put("sqlMapId", "expert.select");
							List<Map<String, Object>> userList = openDao.queryForList(queryMap);
							if (userList.size() > 0) {
								name = name + "," + userList.get(0).get("name").toString() + "";
							}
						} else if ("6".equals(map.get("type").toString())) {
							queryMap.put("id", list.get(i).toString());
							queryMap.put("sqlMapId", "guest.select");
							List<Map<String, Object>> userList = openDao.queryForList(queryMap);
							if (userList.size() > 0) {
								name = name + "," + userList.get(0).get("name").toString() + "";
							}
						}
					}
				}
				return new OutputVo("0000", "密码重置成功", name.replaceFirst(",", ""));
			}else{
				return new OutputVo(jsonData.getString("code"), jsonData.getString("msg"), "");
			}
	    }else {
		  return new OutputVo("4444", "密码重置失败");
	    }
	}
}
